import { Passport, Sm2 } from './passport';
import { Order } from './orders';
import { Captcha } from './captcha';


export { Passport, Order, Captcha };
export type { Sm2 };

export default class WeSkinnyClient {
  private readonly headers: Record<string, string> | undefined;
  private readonly urls: Record<string, string> | undefined;
  private readonly sm2: Sm2 | undefined;

  get order(): Order {
    if (!this._order) {
      this._order = new Order(this.baseUrl, { headers: this.headers, urls: this.urls });
    }
    return this._order;
  }

  get captcha(): Captcha {
    if (!this._captcha) {
      this._captcha = new Captcha(this.baseUrl);
    }
    return this._captcha;
  }

  get passport(): Passport | undefined {
    if (!this._passport && this.sm2) {
      this._passport = new Passport(this.baseUrl, this.sm2, this.headers, this.urls);
    }
    return this._passport;
  }

  private readonly baseUrl: string;
  private _order: Order | undefined;
  private _passport: Passport | undefined;
  private _captcha: Captcha | undefined;

  constructor(baseUrl: string, {
    headers,
    urls, sm2,
  }: { headers?: Record<string, string>, urls?: Record<string, string>, sm2?: Sm2 }) {
    this.baseUrl = baseUrl;
    this.headers = headers;
    this.urls = urls;
    this.sm2 = sm2;
  }
}
